package com.erlin.common.utils;

/**
 * @author Sola
 * @date 2020/06/15
 */

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

/**
 * 国际化工具类
 */
public class MessageUtils {
    final static private MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class);

    /**
     * 用于不含占位符的message
     *
     * @param code
     * @return
     */
    public static String getMessage(String code) {
        return getMessage(code, (Object[]) null);
    }

    /**
     * 用于含有占位符的message
     *
     * @param code 对应messages配置的key
     * @param args 数组参数
     * @return
     */
    public static String getMessage(String code, Object... args) {
        Locale locale = LocaleContextHolder.getLocale();
        return MESSAGE_SOURCE.getMessage(code, args, null, locale);
    }
}